using System;
using System.Web;
using System.Web.UI.WebControls;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.UI;
using System.Collections;

namespace THANHTUNGO.Portal.Modules.MODULE
{
    public partial class ModulePermission : EditItemPage
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonCancel = this.btnCancel;
            base.OnInit(e);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Obtain SiteSettings from Current Context
                PortalConfigurations siteSettings = (PortalConfigurations)HttpContext.Current.Items["PortalConfigurations"];
                PortalConfigurations.ModuleRow mRow = siteSettings.Module.FindByID(this.ModuleID);
                int mdID = mRow.ModuleDefinitionID;
                Guid guid = siteSettings.ModuleDefinition.FindByID(mdID).GeneralDefinitionID;
                sdsFunction.SelectParameters["GUID"].DefaultValue = guid.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArrayList selectedItems = new ArrayList();
            foreach (ListItem item in chkFunction.Items)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Value);
                }
            }
            string[] functions = (string[]) selectedItems.ToArray(typeof(string));
            PortalSecurity.UpdatePermissions(Convert.ToInt32(ddlRole.SelectedValue), this.ModuleID, functions);
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem item in chkFunction.Items)
            {
                item.Selected = false;
            }

            string[] roleFunction = PortalSecurity.SelectFunctionBy_RoleID_ModuleID(Convert.ToInt32(ddlRole.SelectedValue), this.ModuleID);
            foreach (ListItem item in chkFunction.Items)
            {
                foreach (string f in roleFunction)
                {
                    if (item.Value == f)
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }

        }
    }
}